<template>
  <div class="app-container">
    <!-- <img :src="frameDataList" style="float:right;border:1px solid black;width:50%;height: 300px;"> -->
    <CustomComponent />
    <div>
      <img :src="frameDataList" style="border:1px solid black;width:100%;height: 500px;">
      <img :src="anotherImageData" style="border:1px solid black;width:100%;height: 500px;">
    </div>
  </div>
</template>

<script>
import { vedio1, stop1, stop2 } from '@/api/vedio'
// 动态引入图片资源的错误方法
// const urls = "./assets/logo.png";
// 动态引入图片资源的正确方法
export default {
  data() {
    return {
      frameDataList: "/api/video_feed/Camera_0", // 存储帧数据的数组
      anotherImageData: "/api/video_feed/Camera_1"
    };
  },
  methods: {
    stop1() {
      stop1().then(response => {
        this.$message.error('success to stop')
      }).catch(error => {
        this.$message.error('Fail to stop')
      })
    },
    stop2() {
      stop2().then(response => {
        this.$message.error('success to stop')
      }).catch(error => {
        this.$message.error('Fail to stop')
      })
    },
    geturl() {
      vedio1("Camera_0").then(response => {
        const boundary = "--frame"; // 分隔符
        let buffer = ""; // 存储缓冲数据

        response.data.on("data", chunk => {
          buffer += chunk;

          // 检查是否到达分隔符
          const boundaryIndex = buffer.indexOf(boundary);
          if (boundaryIndex !== -1) {
            // 分割出帧数据
            const frameData = buffer.substring(0, boundaryIndex);
            this.frameDataList.push(frameData);

            // 保留剩余的数据，供下一帧使用
            buffer = buffer.substring(boundaryIndex + boundary.length);
          }
          // fetchImageData(response)
        });
      }).catch(error => {
        // 处理错误的响应
        this.$message.error('Failed to get')
      });
    },
    getFrameDataURL(frameData) {
      // 将帧数据转换为DataURL格式
      return "data:image/jpeg;base64," + btoa(frameData);
    }
  },
  created() {
    //this.geturl()
  },
  destroyed() {
    // stop1()
  },
  computed: {
    imageURL() {
      // 对 imageData 进行转换并返回URL
      // 例如：使用 Blob 对象创建URL
      const blob = new Blob([this.imageData], { type: 'image/jpeg' });
      return URL.createObjectURL(blob);
    }
  }

}

</script>

<style lang="scss"></style>

<!-- <template>
  <div class="form">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="desc" label="任务描述">
        <el-input v-model="form.desc" />
      </el-form-item>
      <el-form-item prop="topic_id" label="贴吧帖子id">
        <el-input v-model.number="form.topic_id" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">创建任务</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>s
  </div>
</template>
<script>
import { createSpiderTask } from '@/api/spider'

export default {
  data() {
    return {
      form: {
        topic_id: '',
        start_page: '',
        end_page: '',
        desc: ''
      },
      rules: {
        topic_id: [
          { required: true, message: '帖子id不能为空' },
          { type: 'number', message: '帖子id必须为数字值' }
        ]
      }
    }
  },
  methods: {
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const params = {
            'topic_id': this.form.topic_id,
            'desc': this.form.desc
          }
          createSpiderTask(params).then(response => {
            const h = this.$createElement
            this.$notify({
              title: '任务创建成功',
              message: h('i', { style: 'color: teal' }, '爬虫任务创建成功'),
              duration: 8000
            })
          }).catch(err => {
            console.log(err)
          })
        } else {
          console.log('error submit')
        }
      })
    }
  }
}
</script>

<style scoped>
  .form{
    width: 60%;
    margin: 50px;
  }
</style> -->




